package wwj;

import java.text.DecimalFormat;
import java.util.List;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import wwj.entity.TupleEntity;
import wwj.reader.CsvReader;
import wwj.reader.TabReader;

/*
 * hadoop jar /home/hadoop/桌面/boot.jar UserRankToLike /work/output/UserRankAvg/2/part-r-00000  /work/ratings.csv  /work/output/BookLike/1

 */
public class UserRankToLike {

	public static class MMapper extends
			Mapper<Object, Text, Text, TupleEntity> {

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			List<String> list = CsvReader
					.readLine(value.toString());
			int size=list.size();
			if(size==3){
				if (list.get(0).indexOf("User") != -1)
					return;
				String uid = list.get(0);
				String isbn = list.get(1);
				String rating = list.get(2);
				TupleEntity entity=new TupleEntity();
				entity.setFlag(0);
				entity.setText1(isbn);
				entity.setInt1(Integer.parseInt(rating));
				context.write(new Text(uid),entity );
			}else{
				 list = TabReader
						.readLine(value.toString());
				String uid = list.get(0);
				double avgRank = Double.parseDouble(list.get(1));
				TupleEntity entity=new TupleEntity();
				entity.setFlag(1);
				entity.setDouble1(avgRank);
				context.write(new Text(uid),entity );
			}

		}
	}

	public static class MReducer extends
			Reducer<Text, TupleEntity, Text,Text> {
		private DoubleWritable result = new DoubleWritable();

		public void reduce(Text key, Iterable<TupleEntity> values,
				Context context) throws IOException,
				InterruptedException {
			double rankAvg=0;
			 
			for (TupleEntity val : values) {
				if(val.getFlag().get()==1){
					rankAvg=val.getDouble1().get();
					break;
				} 
			}
			for (TupleEntity val : values) {
				if(val.getFlag().get()==1) continue;
				int rank=val.getInt1().get();
				if(rank>=rankAvg)
				context.write(val.getText1(),key);
			}
			 
		}
	}

	 

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String[] otherArgs = args;
		if (otherArgs.length < 2) {
			System.err.println("Usage:   <in> [<in>...] <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "UserRankToLike");
		job.setJarByClass(UserRankToLike.class);
		 
		job.setMapperClass(MMapper.class);
		job.setReducerClass(MReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(TupleEntity.class);
		job.setOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		for (int i = 0; i < otherArgs.length - 1; ++i) {
			FileInputFormat.addInputPath(job,
					new Path(otherArgs[i]));
		}
		FileOutputFormat.setOutputPath(job, new Path(
				otherArgs[otherArgs.length - 1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
